package ;

import easelhx.display.Container;
import easelhx.display.Graphics;
import easelhx.display.Shape;
import easelhx.js.Image;
import js.Lib;

/**
 * ...
 * @author 
 */

class GraphicsTestTinyExample extends Example
{
	private var img:Image;

	public function new() 
	{
		super();
	}
	
	override public function run():Void 
	{
		super.run();
		
		img = new Image();
        img.onload = layout;
        img.src = "assets/daisy.png";
	}
	
	private function layout() : Void
	{
		var arr = [createStar, createHex, createLineTo, createRadialGradientFill, createEllipse, createRectGradientFill, createBitmapFill];
		var padding:Float = 5;
		var _width:Float = 155;
		var _height:Float = 155;
		var cols:Int = 4;
		var space:Int = 0;
		var l:Int = arr.length;

		var border = createBorder();

		for(i in 0...l) {
			var tile = arr[i]();
			tile.x = 42+(_width + padding) *(i%cols);
			tile.y = 42+(Std.int(i/cols) | 0) * (_height+padding);
			stage.addChild(tile);
		}
		stage.addChild(border);
		stage.update();
	}

    private function createBorder() : Container
	{
		var container = new Container();
		var s:Shape = new Shape();
		s.graphics.bs(img).ss(32).dr(20,20,920,360);
		container.addChild(s);
		return container;
	}

	private function createBitmapFill() : Container
	{
		var container = createTile();
		var s = new Shape();
		s.graphics.bf(img).ss(8).rs(["#FFF","#000"],[0,1],0,0,0,0,30,130).dr(0, 0, 130, 130);
		s.x = 12;
		s.y = 10;
		container.addChild(s);
		return container;
	}

	private function createRectGradientFill() : Container
	{
		var container = createTile();
		var s = new Shape();
		s.graphics.lf(["#FFF","#000"],[0,1],0,0,0,130).dr(0,0,130,130);
		s.x = 12;
		s.y = 10;
		container.addChild(s);
		return container;
	}

	private function createEllipse() : Container
	{
		var container = createTile();
		var s = new Shape();
		s.graphics.f(Graphics.getRGB(0,0x66,0x99,0.5)).ss(4).ls(["#F00","#000"],[0,1],0,0,70,140).de(0,0,70,140.8);
		s.x = 40;
		s.y = 10;
		container.addChild(s);
		return container;
	}

	private function createRadialGradientFill() : Container
	{
		var container = createTile();
		var s = new Shape();
		s.graphics.ss(8).s("#f0f").rf(["#FFF","#0FF"],[0,1],0,0,0,0,0,40).dc(0,0,40);
		s.x = s.y = 80;
		container.addChild(s);
		return container;
	}

	private function createLineTo() : Container
	{
		var container = createTile();
		var s = new Shape();
		s.graphics.ss(16, "round", "round").s("#f90").mt(20,10).lt(90,90).lt(90,140);
		container.addChild(s);
		return container;
	}

	private function createHex() : Container
	{
		var container = createTile();
		var s:Shape = new Shape();
		s.graphics.f("#0F0").dp(0,0,40,6).dp(0,75,40,6);
		s.x = 80;
		s.y = 40;
		container.addChild(s);
		return container;
	}

	private function createStar() : Container
	{
		var container = createTile();
		var s:Shape = new Shape();
		s.graphics.ss(1).s(Graphics.getRGB(255, 255, 0)).f("#FF0").es().dp(0,0,80,5,0.6,-90);
		s.x = 80;
		s.y = 85;
		container.addChild(s);
		return container;
	}

	private function createTile() : Container
	{
		var container = new Container();
		var bg = new Shape();
		bg.graphics.f('#CCCCCC').dr(0, 0, 155, 155).ef();
		bg.alpha = 0.25;
		container.addChild(bg);
		return container;
	}
}